package com.ruoyi.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.base.vo.DbField;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.BaseProperty;
import com.ruoyi.base.service.IBasePropertyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备属性Controller
 * 
 * @author yepanpan
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/base/property")
public class BasePropertyController extends BaseController
{
    @Autowired
    private IBasePropertyService basePropertyService;

    /**
     * 查询所有的设备自定义属性
     */
    @GetMapping("/fields")
    public AjaxResult fields()
    {
        List<DbField> list = basePropertyService.selectDbPropertyList();
        return success(list);
    }

    /**
     * 查询设备属性列表
     */
    @GetMapping("/select")
    public TableDataInfo select(BaseProperty baseProperty)
    {
        List<BaseProperty> list = basePropertyService.selectBasePropertyList(baseProperty);
        list.forEach(p->p.setCode(StringUtils.toCamelCase(p.getCode())));
        return getDataTable(list);
    }

    /**
     * 查询设备属性列表
     */
    @PreAuthorize("@ss.hasPermi('base:property:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseProperty baseProperty)
    {
        startPage();
        List<BaseProperty> list = basePropertyService.selectBasePropertyList(baseProperty);
        return getDataTable(list);
    }

    /**
     * 导出设备属性列表
     */
    @PreAuthorize("@ss.hasPermi('base:property:export')")
    @Log(title = "设备属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseProperty baseProperty)
    {
        List<BaseProperty> list = basePropertyService.selectBasePropertyList(baseProperty);
        ExcelUtil<BaseProperty> util = new ExcelUtil<BaseProperty>(BaseProperty.class);
        util.exportExcel(response, list, "设备属性数据");
    }


    /**
     * 获取设备属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:property:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(basePropertyService.selectBasePropertyById(id));
    }

    /**
     * 新增设备属性
     */
    @PreAuthorize("@ss.hasPermi('base:property:add')")
    @Log(title = "设备属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BaseProperty baseProperty)
    {
        return toAjax(basePropertyService.insertBaseProperty(baseProperty));
    }

    /**
     * 修改设备属性
     */
    @PreAuthorize("@ss.hasPermi('base:property:edit')")
    @Log(title = "设备属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BaseProperty baseProperty)
    {
        return toAjax(basePropertyService.updateBaseProperty(baseProperty));
    }

    /**
     * 删除设备属性
     */
    @PreAuthorize("@ss.hasPermi('base:property:remove')")
    @Log(title = "设备属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(basePropertyService.deleteBasePropertyByIds(ids));
    }
}
